<template>
  <div>
    <h2 align="center">用户--投资赎回</h2>
    <table class="table table-bordered">
      <tr>
        <td align="right">项目名称:</td>
        <td>
          {{ list.I_Name }}
        </td>
      </tr>

      <tr>
        <td align="right">项目投资总金额:</td>
        <td>
          {{ list.I_Budget }}
        </td>
      </tr>

      <tr>
        <td align="right">用户已投金额:</td>
        <td>
         {{ list.UI_Money }}
        </td>
      </tr>

      <tr>
        <td align="right">赎回金额:</td>
        <td>
          <input type="text" v-model="UIR_Money">
        </td>
      </tr>

      <tr>
        <td></td>
        <td>
          <input type="button" value="确认赎回" @click="MakeRedeem" class="btn btn-primary">
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">

import {reactive,toRefs,onMounted} from 'vue';

import { useRouter,useRoute } from 'vue-router';

import {useUserStore} from '../../store/user'

import axios from 'axios';

let userStore = useUserStore();

//路由
let router = useRouter();
let route = useRoute();


//数据
let data:any =reactive({
  list:{},
   UIR_Money:''
})

//解构
let {list,UIR_Money} = toRefs(data);


//生命周期
onMounted(()=>{
  getData()
})

//回显
//反填
const getData = ()=>{
  axios({
    url:'/api/Invest/GetUserRedeemByUId',
    method:'get',
    params:{
      UI_Id:route.query.UI_Id
    }
  }).then(res=>{
    console.log(res);
    list.value=res.data.data[0]
  }).catch(err=>{
    console.log(err);
    
  })
}

//确认赎回
const MakeRedeem = ()=>{
   //非空判断
   if(UIR_Money.value ==''){
    return alert('赎回金额不能为空')
   }
   //判断投资金额是否为数字
   if(isNaN(Number(UIR_Money.value))==true){
    return alert('赎回金额必须为数字');
   }
   if(UIR_Money.value>list.value.UI_Money){
    return alert('赎回金额不能大于已投资金额');
   }
   
   //发送网络请求
   axios({
      url:'/api/Invest/PostAddUserRedeem',
      method:'post',
      data:{
        UIR_Money:UIR_Money.value,
        UI_Id:route.query.UI_Id
      }
   }).then(res=>{
      console.log(res);
      //判断是否成功
      if(res.data.data==-1){
        return alert('赎回金额不能大于已投资金额')
      }else if(res.data.data>0){
         alert('赎回成功');
         //跳转用户投资列表
         router.push('/main/userInvestRedeemList');
      }else{
         return alert('赎回失败');
      }
   }).catch(err=>{
      //异常处理机制
      console.log(err);
   })
}

</script>

<style scoped>

</style>